/*
 * QE3e Qt Quick 3D Engine.
 * Copyright (C) 2010 Joris Vaillant and Mathieu Pujol
 * <joris.vaillant@gmail.com>  <mathieu.pujol87@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "MonQGLWidget.h"

#include <QDebug>
#include <QKeyEvent>
#include <LightItem.h>


MonQGLWidget::MonQGLWidget()
{
  tmp=0;


}

using namespace qe3e;

void MonQGLWidget::paintGL()
{

  mSceneDrawer.setViewport(0,0,this->width(),this->height());

  QList<LightDescriptor> mDebugLights;
  LightDescriptor tmp;
  tmp.mAmbient[0]=0.25f;
  tmp.mAmbient[1]=0.25f;
  tmp.mAmbient[2]=0.25f;
  tmp.mAmbient[3]=1.f;

  tmp.mDiffuse[0]=0.8f;
  tmp.mDiffuse[1]=0.8f;
  tmp.mDiffuse[2]=0.8f;
  tmp.mDiffuse[3]=1.f;

  tmp.mSpecular[0]=1.f;
  tmp.mSpecular[1]=1.f;
  tmp.mSpecular[2]=1.f;
  tmp.mSpecular[3]=1.f;

  tmp.mPosition[0]=0.f;
  tmp.mPosition[1]=0.f;
  tmp.mPosition[2]=5.f;
  tmp.mPosition[3]=1.f;

  //    tmp.mSpotCutOffAngle=5.;
  //    tmp.mDirection[0]=0.f;
  //    tmp.mDirection[1]=-1.f;
  //    tmp.mDirection[2]=0.f;
  mDebugLights<<tmp;
 mSceneDrawer.drawScene(mScene.items(),mDebugLights);
}

void MonQGLWidget::keyPressEvent(QKeyEvent * event)
{

  QGLWidget::keyPressEvent(event);
  qe3e::ITEM_FX_ID mask = 0x3;
  switch(event->key())
  {
  case Qt::Key_H:
    qDebug()<<"Key help not Ctrl / Ctrl => false / true";
    qDebug()<<"W => wireframe";
    qDebug()<<"P => point";
    qDebug()<<"G => gouraut / flat shading";
    qDebug()<<"T => texturing";
    qDebug()<<"L => lightning";
    break;
  case Qt::Key_W:
    if(event->modifiers() == Qt::ControlModifier)
    {
      mSceneDrawer.setWireframe(false,mask);
      qDebug()<<"Disable wireframe";
    }
    else
    {
      mSceneDrawer.setWireframe(true,mask);
      qDebug()<<"Enable wireframe";
    }
    break;
  case Qt::Key_P:
    if(event->modifiers() == Qt::ControlModifier)
    {
      mSceneDrawer.setPoint(false,mask);
      qDebug()<<"Disable point";
    }
    else
    {
      mSceneDrawer.setPoint(true,mask);
      qDebug()<<"Enable point";
    }
    break;
  case Qt::Key_G:
    if(event->modifiers() == Qt::ControlModifier)
    {
      mSceneDrawer.setFlatShading(false,mask);
      qDebug()<<"Disable flat shading";
    }
    else
    {
      mSceneDrawer.setFlatShading(true,mask);
      qDebug()<<"Enable flat shading";
    }
    break;
  case Qt::Key_T:
    if(event->modifiers() == Qt::ControlModifier)
    {
      mSceneDrawer.setTexturing(false,mask);
      qDebug()<<"Disable texturing";
    }
    else
    {
      mSceneDrawer.setTexturing(true,mask);
      qDebug()<<"Enable texturing";
    }
    break;
  case Qt::Key_L:
    if(event->modifiers() == Qt::ControlModifier)
    {
      mSceneDrawer.setLighting(false,mask);
      qDebug()<<"Disable lightning";
    }
    else
    {
      mSceneDrawer.setLighting(true,mask);
      qDebug()<<"Enable lightning";
    }
    break;
  case Qt::Key_Space:
    updateGL();
    break;
  case Qt::Key_D:
    mSceneDrawer.setMaskItemFxToUse(0x1);
    break;
  default:
    break;
  }
}


